{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "is_executing": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/Users/yzs/opt/lib/miniconda3/envs/py10agent/bin/python\n"
     ]
    }
   ],
   "source": [
    "# 下载vscode：https://code.visualstudio.com/\n",
    "# 下载miniconda：https://docs.conda.io/en/latest/miniconda.html\n",
    "# 创建虚拟环境：conda create -n py10agent python=3.10\n",
    "# 激活虚拟环境：conda activate py10agent\n",
    "# 输出当前的python编译器路径(该命令只在mac和linux下生效)\n",
    "!which python"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c2e7b771",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入openai库\n",
    "import openai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d3917883",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.28.1\n"
     ]
    }
   ],
   "source": [
    "print(openai.version.VERSION)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "55633d36",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 没置API-Key，这里需要换成自己的API-KEY\n",
    "openai.api_key = \"sk-G4Q71zukVEF3XF1YYL0WsC06-kxy3sGo8HSA2zPwC2k=G4Q7\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6c8ed1d2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设置国内反向代理地站，如果不配置则为 https://api.openai.com/v1\n",
    "# 设置了之后无需代理即可调用GPT模型\n",
    "openai.api_base = \"http://ai-free-hub.cn:9000/v1\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e541e108",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你好！有什么我可以帮助你的吗？'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 调用GPT模型\n",
    "response = openai.ChatCompletion.create(\n",
    "    model=\"Qwen-14B-Chat-Int4\",\n",
    "    messages=[\n",
    "        {\"role\": \"user\", \"content\": \"你好\"}\n",
    "    ]\n",
    ")\n",
    "response.choices[0].message['content']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8ff8ca4b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'id': '', 'object': 'chat.completions', 'created': 1705119525, 'model': 'gpt-3.5-turbo', 'choices': [{'index': 0, 'message': {'role': 'assistant', 'content': '您还未使用激活码激活账户。'}, 'finish_reason': 'stop'}], 'usage': {'prompt_tokens': 16, 'total_tokens': 16, 'completion_tokens': 16, 'first_tokens': 16}}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "url = \"http://192.168.0.27:9000/v1/chat/completions\"\n",
    "\n",
    "payload = json.dumps({\n",
    "  \"model\": \"Qwen-14B-Chat-Int4\",\n",
    "  \"messages\": [\n",
    "    {\n",
    "      \"role\": \"user\",\n",
    "      \"content\": \"你好\"\n",
    "    }\n",
    "  ]\n",
    "})\n",
    "headers = {'Authorization': 'Bearer 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'}\n",
    "\n",
    "response = requests.post(url, headers=headers, data=payload)\n",
    "\n",
    "print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "0aeaba9f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'id': '',\n",
       " 'object': 'chat.completions',\n",
       " 'created': 1705119525,\n",
       " 'model': 'gpt-3.5-turbo',\n",
       " 'choices': [{'index': 0,\n",
       "   'message': {'role': 'assistant', 'content': '您还未使用激活码激活账户。'},\n",
       "   'finish_reason': 'stop'}],\n",
       " 'usage': {'prompt_tokens': 16,\n",
       "  'total_tokens': 16,\n",
       "  'completion_tokens': 16,\n",
       "  'first_tokens': 16}}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response.json()\n",
    "# 字段id用于标识本次请求，我的模型部署方式为Fastchat，该库未实现该字段\n",
    "# object: 对于所有gpt3.5和gpt4都划分到chat.completions\n",
    "# created: 线程标识\n",
    "# choices: 大模型返回的对象. index：表示返回答案的索引。message：返回的信息。role：message的角色。更完整的信息可以参考我们的官网:http://ai-free-hub.cn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "138dd883",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'prompt_tokens': 16,\n",
       " 'total_tokens': 16,\n",
       " 'completion_tokens': 16,\n",
       " 'first_tokens': 16}"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response.json()[\"usage\"] # token的消耗量"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
